package TankGame07;

import java.io.*;
import java.util.Vector;

public class Recorder {
    private static int hitEnemyNum = 0;//击毁敌方坦克数量
    private static BufferedWriter bw= null;//输出流，用于保存文件
    private static BufferedReader br = null;//输入流，从文件中读取敌方坦克数据(坐标，方向)
    private static String filePath = "src\\TankGame07\\tankgameRec.txt";//存储游戏记录
    private static Vector<EnemyTank> enemyTanks = null;//获得集合中当前存活的敌方坦克对象
    private static Vector<Node> nodes = new Vector<>();//使用nodes集合保存从文件中读取敌方坦克数据(坐标，方向)

    public static Vector<EnemyTank> getEnemyTanks() {
        return enemyTanks;
    }

    public static String getFilePath() {
        return filePath;
    }

    public static void setFilePath(String filePath) {
        Recorder.filePath = filePath;
    }

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    public static int getHitEnemyNum() {
        return hitEnemyNum;
    }

    public static void setHitEnemyNum(int hitEnemyNum) {
        Recorder.hitEnemyNum = hitEnemyNum;
    }

    public static void addHitEnemyNum(){
        hitEnemyNum++;//击毁敌方坦克数加一
    }

    public static void savehitEnemyNum() throws IOException {
        //使用BufferedWriter输出流，把击毁敌方坦克数量记录到文件中
        //当窗口关闭的时候就写入数据到文件
        bw = new BufferedWriter(new FileWriter(filePath));
        bw.write(hitEnemyNum+"\n");//需要换行不然保存的数字是乱码
        if(enemyTanks != null){
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                bw.write(enemyTank.getX()+" "+enemyTank.getY()+" "+enemyTank.getDirect());
                bw.write("\r\n");
            }
        }
        bw.close();//关闭流
        System.out.println("游戏成功保存到d:\\score.txt...");
    }

    public static Vector<Node> readGameRecord() throws IOException {
        //调用该方法读取存档文件中敌方坦克的坐标和方向
        //使用nodes集合保存,
        br = new BufferedReader(new FileReader(filePath));
        hitEnemyNum = Integer.valueOf(br.readLine());//先读取击击毁敌方坦克数
        String line = "";
        while ((line = br.readLine()) != null){
            String[] split = line.split(" ");
            Node node = new Node(Integer.valueOf(split[0]), Integer.valueOf(split[1]), Integer.valueOf(split[2]));
            nodes.add(node);//将node添加到集合中保存
        }
        br.close();//关闭流
        return nodes;
    }
}
